import request from '@/utils/request'
import defaultSettings from '@/config/defaultSettings'
import store from "@/store"

async function getDictInfo (dictCode,params) {
    return request.get('/SysDictController/getDictInfo',{params:{dictCode:dictCode,dictParam:params}})
}
export async function initDict(dictCodes,params){
    if(!params){
        params=''
    }
    if(!dictCodes){
        return
    }

    let dictArr = dictCodes.split(",")
    let newDictArr = []
    if(defaultSettings.isCacheDict){
        for(var dict of dictArr){
            let stateDictInfo = store.state.dict.dictInfo[dict]
            if(!stateDictInfo){
                newDictArr.push(dict)
            }
        }
    }else{
        newDictArr = dictArr
    }

    if(newDictArr.length>0){
        await getDictInfo(newDictArr.join(","),params).then(res => {
            if(res.success){
                for(let d in res.result){
                    store.dispatch("setDictInfo",{dictCode:d,dictInfo:res.result[d]}) 
                }
               
            }
        }).catch(error => {
           
        })
    }
}

/*
export function initDict(dictCodes,params){
    if(!params){
        params=''
    }
    if(!dictCodes){
        return new Promise((resolve, reject) => {resolve()})
    }
    return new Promise((resolve, reject) => {
   
        let dictArr = dictCodes.split(",")
        let newDictArr = []
        if(defaultSettings.isCacheDict){
            for(var dict of dictArr){
                let stateDictInfo = store.state.dict.dictInfo[dict]
                if(!stateDictInfo){
                    newDictArr.push(dict)
                }
            }
        }else{
            newDictArr = dictArr
        }

        if(newDictArr.length>0){
            getDictInfo(newDictArr.join(","),params).then(res => {
                    if(res.success){
                        for(let d in res.result){
                            store.dispatch("setDictInfo",{dictCode:d,dictInfo:res.result[d]}) 
                        }
                        resolve()
                    }else{
                        reject()
                    }
                }).catch(error => {
                    reject(error)
                })
        }else{
            resolve()
        }
    }).then(res=>{
       
    }).catch(err=>{
       
    })
}

*/